package com.zzyl.serve.service.impl;

import java.util.List;
import java.util.Arrays;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.serve.domain.NursingLevel;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.service.INursingLevelService;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * 护理计划Service业务层处理
 *
 * @author zzyl
 * @date 2025-10-13
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper,NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private INursingProjectPlanService inursingProjectPlanService;

    @Autowired
    private INursingLevelService iNursingLevelService;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id)
    {
        //根据id查询护理计划信息
        NursingPlan nursingPlan = this.getById(id);
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan,NursingPlanVo.class);
        //获取中间表信息
        List<NursingProjectPlan> projectPlanList = inursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        List<NursingProjectPlanVo> nursingProjectPlanVoList = projectPlanList.stream().map(nursingProjectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = BeanUtil.toBean(nursingProjectPlan, NursingProjectPlanVo.class);
            nursingProjectPlanVo.setProjectId(nursingProjectPlan.getProjectId().toString());
            nursingProjectPlanVo.setExecuteCycle(nursingProjectPlan.getExecuteCycle().toString());
            return nursingProjectPlanVo;
        }).toList();
        nursingPlanVo.setProjectPlans(nursingProjectPlanVoList);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto dto)
    {
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        boolean result = this.save(nursingPlan);
        //保存中间表信息
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        projectPlans.forEach(projectPlan -> {
            if (projectPlan.getProjectId() == null) {
                throw new RuntimeException("护理项目不能为空，请选择护理项目");
            }else if (!StringUtils.hasLength(projectPlan.getExecuteTime())) {
                throw new RuntimeException("期望服务时间不能为空，请选择");
            }else if (projectPlan.getExecuteCycle() == null) {
                throw new RuntimeException("执行周期不能为空，请选择");
            } else if (projectPlan.getExecuteFrequency() == null) {
                throw new RuntimeException("执行频次不能为空");
            }
        });
        if (!CollectionUtils.isEmpty(projectPlans)){
            projectPlans.forEach(nursingProjectPlan -> nursingProjectPlan.setPlanId(nursingPlan.getId()));
            result = inursingProjectPlanService.saveBatch(projectPlans);
        }
        //保存护理计划信息
        return  result ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto dto)
    {
        //更新护理计划信息
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        Boolean result = this.updateById(nursingPlan);

        //更新中间表
        List<NursingProjectPlan> projectPlanList = dto.getProjectPlans();
        if (!CollectionUtils.isEmpty(projectPlanList)){
            inursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlan.getId()));
            projectPlanList.forEach(nursingProjectPlan -> nursingProjectPlan.setPlanId(nursingPlan.getId()));
            result = inursingProjectPlanService.saveBatch(projectPlanList);
        }
        return result ? 1 : 0;
    }

    @Override
    public List<NursingPlan> getAll() {
        return this.list();
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Integer[] ids)
    {
        return this.removeByIds(Arrays.asList(ids)) ?1:0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNursingPlanById(Integer id)
    {
        //判断当前的状态是否为启用
        NursingPlan nursingPlan = this.getById(id);
        if (nursingPlan.getStatus() == 1){
            throw new RuntimeException("启用状态下不能删除");
        }

        //判断当护理计划是否被护理等级关联
        List<NursingLevel> nursingLevels = iNursingLevelService.list(Wrappers.<NursingLevel>lambdaQuery().eq(NursingLevel::getLplanId, id));
        if (!CollectionUtils.isEmpty(nursingLevels)){
            throw new RuntimeException("当前护理计划被护理等级关联，不可删除");
        }

        //删除护理计划
        Boolean result = this.removeById(id);
        //删除中间表
        List<NursingProjectPlan> projectPlanList = inursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        if (!CollectionUtils.isEmpty(projectPlanList)){
            result = inursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        }
        return result?1:0;
    }
}
